package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UIDUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlpContractDetailMapper;
import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 合同表管理Service业务层处理
 * 
 * @author zhangjun
 * @date 2020-02-13
 */
@Service
public class AlpContractDetailServiceImpl implements IAlpContractDetailService 
{
    private static final Logger logger = LoggerFactory.getLogger(AlpContractDetailServiceImpl.class);

    @Autowired
    private AlpContractDetailMapper alpContractDetailMapper;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询合同表管理
     * 
     * @param contractId 合同表管理ID
     * @return 合同表管理
     */
    @Override
    public AlpContractDetail selectAlpContractDetailById(Long contractId)
    {
        return alpContractDetailMapper.selectAlpContractDetailById(contractId);
    }

    /**
     * 查询合同表管理
     *
     * @param stageId 阶段ID
     * @return 合同表管理
     */
    @Override
    public AlpContractDetail selectAlpContractByStageId(Long stageId)
    {
        return alpContractDetailMapper.selectAlpContractByStageId(stageId);
    }


    /**
     * 查询合同表管理列表
     * 
     * @param alpContractDetail 合同表管理
     * @return 合同表管理
     */
    @Override
    @DataScope(deptAlias = "con", userAlias = "con")
    public List<AlpContractDetail> selectAlpContractDetailList(AlpContractDetail alpContractDetail)
    {
//        一、bidding年度为2020年以前的项目，最新未回款金额=20年初合同余额-截止当前已回款
//        bidding年度为2020年以后的项目，最新未回款金额=合同金额-截止当前已回款
//        二、已回款金额=合同金额-最新未回款金额
        List<AlpContractDetail> listResult = new ArrayList<>();
        List<AlpContractDetail> list = alpContractDetailMapper.selectAlpContractDetailList(alpContractDetail);
        for (AlpContractDetail alpContract : list) {
            try {
                if(StringUtils.isEmpty(alpContract.getContractSignedInyear())) {
                    logger.error("数据级告警，本条数据初始化有问题，合同签订年度为空,{}",JSONObject.toJSON(alpContract));
                }
                if ("2020年以前".equals(alpContract.getContractSignedInyear())) {
                    alpContract.setNotReceivedMoney(alpContract.getBalanceSince2020().subtract(alpContract.getHasReceivedMoneyNow()));

                } else {
                    alpContract.setNotReceivedMoney(alpContract.getContractMoney().subtract(alpContract.getHasReceivedMoneyNow()));
                }
                alpContract.setHasReceivedMoney(alpContract.getContractMoney().subtract(alpContract.getNotReceivedMoney()));
                listResult.add(alpContract);

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("合同列表展示异常，该数据：{}", JSONObject.toJSON(alpContract));
            }
        }

        return listResult;
    }

    public Integer selectAllCount(AlpContractDetail alpContractDetail) {
        return alpContractDetailMapper.selectAllCount(alpContractDetail);
    }
    /**
     * 新增合同表管理
     * 
     * @param alpContractDetail 合同表管理
     * @return 结果
     */
    @Override
    public int insertAlpContractDetail(AlpContractDetail alpContractDetail)
    {

        alpContractDetail.setDelFlag("0");
        alpContractDetail.setUserId(ShiroUtils.getSysUser().getUserId());
        SysUser user = userService.selectUserById(Long.valueOf(alpContractDetail.getXsPrincipalId()));
        if (null != user) {
            alpContractDetail.setXsPrincipalName(user.getLoginName());
            alpContractDetail.setDeptId(user.getDeptId());
            SysDept dept = deptService.selectDeptById(user.getDeptId());
            alpContractDetail.setXsDepartment(dept.getDeptName());
        }


        return alpContractDetailMapper.insertAlpContractDetail(alpContractDetail);
    }

    /**
     * 修改合同表管理
     * 
     * @param alpContractDetail 合同表管理
     * @return 结果
     */
    @Override
    public int updateAlpContractDetail(AlpContractDetail alpContractDetail)
    {
        return alpContractDetailMapper.updateAlpContractDetail(alpContractDetail);
    }

    /**
     * 删除合同表管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlpContractDetailByIds(String ids)
    {
        return alpContractDetailMapper.deleteAlpContractDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除合同表管理信息
     * 
     * @param contractId 合同表管理ID
     * @return 结果
     */
    @Override
    public int deleteAlpContractDetailById(Long contractId)
    {
        return alpContractDetailMapper.deleteAlpContractDetailById(contractId);
    }

    @Override
    public int getCount(AlpContractDetail alpContractDetail) {
        return alpContractDetailMapper.getCount(alpContractDetail);
    }
}
